﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using Microsoft.Practices.ObjectBuilder;
using Microsoft.Practices.CompositeWeb;
using WCSFContrib.ECommerceCatalog.BusinessEntities;
using WCSFContrib.ECommerceCatalog.BusinessEntities.Enums;

namespace WCSFContrib.ECommerceCatalog.User.Views
{
    public class CheckoutPresenter : Presenter<ICheckoutView>
    {
        private IUserController _controller;
        private string _userName;

        public string UserName
        {
            set { _userName = value; }
        }
        
        public CheckoutPresenter([CreateNew] IUserController controller)
        {
            this._controller = controller;
            this._controller.StartUserPageFlow();
            this._controller.CurrentPage = "Checkout";
        }

        public override void OnViewInitialized()
        {
            UserProfile userProfile = _controller.GetUserProfile(_userName);

            if (userProfile == null)
                return;
            
            View.CurrentUser = userProfile.User;
            View.OrderDate = DateTime.Now;
            View.AddressList = userProfile.Addresses;
            View.PaymentsMethodsList = userProfile.PaymentsMethods;
        }

        public void OnCompleteCheckout(string userName)
        {
            Address billingAddress = _controller.GetAddresById(View.BillingAddressId);
            Address shippingAddress = _controller.GetAddresById(View.ShippingAddressId);
            Payment paymentMethod = _controller.GetPaymentById(View.PaymentMethodId);

            _controller.OrderCheckout(userName, View.OrderDate, billingAddress, shippingAddress, paymentMethod);
        }

        public void OnGoToUserProfilePage()
        {
            _controller.GoUserProfilePage();
        }
    }
}




